#include "ColorTools.h"
#include <iostream>

using namespace OBR;

char4 ColorTools::radiance2color(Spectrum radiance, float gamma)
{
    radiance = radiance.pow(1 / gamma);
    return spectrum2color(radiance);
}

char4 ColorTools::spectrum2color(Spectrum s)
{
    switch (s.nSamples)
    {
    case 3:
        // simple RGB case
        return make_char4(clamp((int)roundf(s[0] * 256), 0, 255),
                          clamp((int)roundf(s[1] * 256), 0, 255),
                          clamp((int)roundf(s[2] * 256), 0, 255), 255u);
    case 1:
        return make_char4(clamp((int)roundf(s[0] * 256), 0, 255),
                          clamp((int)roundf(s[0] * 256), 0, 255),
                          clamp((int)roundf(s[0] * 256), 0, 255), 255u);
    default:
        std::cerr << "this sample number of spectrum to color not implemented" << std::endl;
        return make_char4(0, 0, 0, 255u);
    }
}